package com.javaeye.dengyin2000.fgcrawler.utils;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;

/**
 * User: deng.yin
 * Date: 2009-9-27
 * Time: 20:54:16
 */
public class DUtils {

    public static final String IE7_USERAGENT = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)";
    public static final String FF3_USERAGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3";


    public static String getHtmlSource(String url, String charset) {
        HttpMethod httpMethod = new GetMethod(url);
        HttpClient httpClient = new HttpClient();
		HttpClientParams clientParams = httpClient.getParams();
		clientParams.setSoTimeout(15000);
		httpMethod.getParams().setSoTimeout(15000);
		httpMethod.addRequestHeader("Connection", "close");
		httpMethod.addRequestHeader("User-Agent", FF3_USERAGENT);
        httpMethod.setFollowRedirects(true);

		// clientParams.setBooleanParameter("http.protocol.expect-continue",
		// false);
		HttpConnectionManagerParams connectionParams = httpClient
				.getHttpConnectionManager().getParams();
		 connectionParams.setConnectionTimeout(15000);
		// connectionParams.setStaleCheckingEnabled(false);
		for (int i = 0; i < 1; i++) {
            try {
                    int httpStatus = httpClient.executeMethod(httpMethod);
                    if (httpStatus == HttpStatus.SC_OK ) {
                        byte[] bodybytes = httpMethod.getResponseBody();
                        String htmlSource = new String(bodybytes, charset);

                        return htmlSource;
                    } else {
    					try {
    						Thread.sleep(2000 * (i+1));
    					} catch (InterruptedException e) {
    					}
                    }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {

                }
            } catch (IOException e) {
    					try {
    						Thread.sleep(1000 * (i+1));
    					} catch (InterruptedException e1) {
    					}
            } finally {
                if (httpMethod != null)
                    httpMethod.releaseConnection();
            }
		}
		return null;
    }
}
